package com.wangkang.eduservice.controller;
import com.wangkang.commonutils.R;
import com.wangkang.eduservice.entity.EduCourse;
import com.wangkang.eduservice.entity.vo.EduCourseInfoVo;
import com.wangkang.eduservice.entity.vo.EduCoursePublishVo;
import com.wangkang.eduservice.enums.CourseStatusEnum;
import com.wangkang.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangkang
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(tags = "课程管理")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private  EduCourseService courseService;

    public EduCourseController(EduCourseService courseService) {
        this.courseService = courseService;
    }

    @ApiOperation("添加课程信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody EduCourseInfoVo courseInfoVo){

        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        EduCourseInfoVo courseInfoVo = courseService.getCourseInfoByCourseId(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody EduCourseInfoVo courseInfoVo){
        courseService.updateCourse(courseInfoVo);
        return R.ok();
    }


    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        EduCoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);

        return R.ok().data("coursePublishInfo",coursePublishVo);
    }

    @ApiOperation("课程发布")
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        //添加课程信息时并未设定课程的id值，在课程发布时设定课程的id值(随机产生)
        course.setId(id);
        //并修改课程的状态为已发布
        course.setStatus(CourseStatusEnum.NORMAL.getDesc());
        //根据课程的id修改课程状态
        courseService.updateById(course);
        return R.ok();
    }

    @ApiOperation("查询课程列表")
    @GetMapping
    public R getCourseList(){

        //TODO 完善条件查询带分页
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        //TODO 前端实现
        courseService.deleteCourse(courseId);
        return R.ok();
    }


}